<?php

namespace app\modules\groups\controllers;

use Yii;
use app\modules\groups\models\Group;
//use app\modules\groups\models\query\GroupQuery;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\modules\groups\models\TreeForm;
use app\modules\groups\models\TreeSeach;

/**
 * GroupController implements the CRUD actions for Group model.
 */
class GroupController extends Controller {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Group models.
     * @return mixed
     */
    public function actionIndex() {
//        $searchModel = new GroupQuery();
        $searchModel = new TreeSeach();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Group model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Group model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new TreeForm();
//         $aa = Yii::$app->user->can('管理员');
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * by liu 存储组织一级
     */
    public function actionTestGroup() {
        //第一级saveOne   修改数组，id
        //第二级//saveTow 修改数组，id
        //注释
//        if(TreeForm::saveTow()){
//            echo '成功';
//        }else{
//            echo '失败';
//        }
    }

    /**
     * Updates an existing Group model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->groupID]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Group model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * 组织结构页面ifrom
     * @return type
     */
    public function actionGroups_search() {
        $this->layout = '@app/views/layouts/search_groups';
        return $this->render('groups_search', [
                    'model' => null,
        ]);
    }

    /**
     * 获取组织搜索
     */
    public function actionOrganization() {
        $cache = Yii::$app->cache;
        $data = $cache->get('cache_data_groups');
        if ($data === false) {
            //这里我们可以操作数据库获取数据，然后通过$cache->set方法进行缓存 

            $array = [];
            $arrayModel = [];
            $model = TreeForm::find()->all();
//            foreach ($model as $value){
////                $company = explode(',', $value->group_mark);
//                $array = ['id'=>$value->groupID,'pId'=>$value->parentGroupID,'name'=>$value->groupName];
//                array_push($arrayModel, $array);
//            }
//            $array1 = ['id'=>2,'pId'=>1,'name'=>'通金所'];
//            $array2 = ['id'=>99999,'pId'=>2,'name'=>'金泰'];
//            $array3 = ['id'=>99998,'pId'=>2,'name'=>'华东区'];
//            $array4 = ['id'=>99997,'pId'=>2,'name'=>'作业管理部'];
//            $array5 = ['id'=>99996,'pId'=>2,'name'=>'其它'];
//            echo '<pre>';
//            print_r($arrayModel);die;
//            echo '</pre>';
            if (count($model) > 0) {
                foreach ($model as $value) {
                    if ($value->root == 0) {
                        $array = ['id' => $value->id, 'pId' => $value->root, 'name' => $value->name, 'open' => true];
                    } else {
                        $array = ['id' => $value->id, 'pId' => $value->root, 'name' => $value->name];
                    }

                    array_push($arrayModel, $array);
                }
                $cacheData = json_encode(['status' => 'success', 'groups' => $arrayModel]);
                $cache->set('cache_data_groups', $arrayModel, 60 * 60 * 24);
               
                echo $cacheData;
            } else {
                echo json_encode(['status' => 'error', 'message' => '获取组织失败!']);
            }
            //set方法的第一个参数是我们的数据对应的key值，方便我们获取到 
            //第二个参数即是我们要缓存的数据 
            //第三个参数是缓存时间，如果是0，意味着永久缓存。默认是0 
        } else {
            echo json_encode(['status' => 'success', 'groups' => $data]);
        }
    }

    /**
     * Finds the Group model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Group the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Group::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
